MOD=sloop
NUM=0
SEC=10
WC=100
RC=10

ifneq ($(KERNELRELEASE),)
	obj-m	:= $(MOD).o
	sloop-y := loop.o bitmap.o
else
	KERNELDIR ?= /lib/modules/$(shell uname -r)/build
	PWD := $(shell pwd)
	MYCFLAGS = -Wall

all: testbm diskmig module

module:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) modules

testbm: testbm.o
	$(CC) -o $@ $<

testbm.o: testbm.c bitmap.c
	$(CC) $(MYCFLAGS) -c -o $@ $<

diskmig: diskmig.o
	$(CC) -o $@ $<

diskmig.o: diskmig.c loop.h
	$(CC) $(MYCFLAGS) -c -o $@ $<
	
clean:
	rm -r *.ko *.mod.c *.o .*o.cmd .*o.d .tmp_versions Module.symvers|| true
	rm testbm diskmig || true
	sudo rm 1.img 2.img 1.map || true

up: all
	sudo umount /dev/loop$(NUM) 2> /dev/null || true
	sudo losetup -d /dev/loop$(NUM) 2> /dev/null || true
	sudo rmmod loop 2> /dev/null || true
	sudo rmmod $(MOD) 2> /dev/null || true
	sudo insmod $(MOD).ko
	sleep 1

uporig:
	sudo losetup -d /dev/loop$(NUM) 2> /dev/null || true
	sudo rmmod $(MOD) 2> /dev/null || true
	sudo modprobe loop
	sleep 1

down:
	sudo umount /dev/loop$(NUM) 2> /dev/null || true
	sudo losetup -d /dev/loop$(NUM) 2> /dev/null || true
	sudo rmmod $(MOD) || true

1.img:
	sudo dd if=/dev/zero of=1.img bs=1M seek=1023 count=1
	yes | sudo mkreiserfs -f 1.img

2.img:
	cp 1.img 2.img

test0: up
	cat /proc/devices | grep "7 loop"

test1: test0
	dmesg | tail | grep "max_loop"

test2: test1
	ls -d /sys/block/loop*  | wc -l

test3: test2 1.img
	sudo losetup /dev/loop$(NUM) 1.img

test4: test3
	sudo losetup /dev/loop$(NUM)

test5: test4
	sudo losetup -d /dev/loop$(NUM)

test6: up 1.img
	sudo losetup /dev/loop$(NUM) 1.img
	sudo cat /dev/loop$(NUM) || true
	dmesg | grep "no r/w implemented" | wc -l

test7: up 1.img
	sudo losetup /dev/loop$(NUM) 1.img
	sudo dd if=/dev/loop$(NUM) of=/dev/null

test7orig: uporig 1.img
	sudo losetup /dev/loop$(NUM) 1.img
	sudo dd if=/dev/loop$(NUM) of=/dev/null

test8: up 1.img
	sudo losetup /dev/loop$(NUM) 1.img
	sudo mount /dev/loop$(NUM) /mnt/temp
	sudo dbench 1 -t $(SEC) -D /mnt/temp
	sudo umount /dev/loop$(NUM)

test8orig: uporig 1.img
	sudo losetup /dev/loop$(NUM) 1.img
	sudo mount /dev/loop$(NUM) /mnt/temp
	sudo dbench 1 -t $(SEC) -D /mnt/temp
	sudo umount /dev/loop$(NUM)

test9: up diskmig 1.img
	sudo losetup /dev/loop$(NUM) 1.img
	sudo mount /dev/loop$(NUM) /mnt/temp
	sudo ./diskmig -w /dev/loop$(NUM)
	sudo dbench 1 -t $(SEC) -D /mnt/temp
	sudo umount /dev/loop$(NUM)
	cat /proc/sloop/loop$(NUM)/wlimit_count
	sudo ./diskmig -x /dev/loop$(NUM) 1.map
	sudo ./diskmig -m 1.map
	
test10: up diskmig 1.img
	sudo losetup /dev/loop$(NUM) 1.img
	sudo mount /dev/loop$(NUM) /mnt/temp
	sudo ./diskmig -w /dev/loop$(NUM)
	sudo cp 1.img 2.img
	sudo dbench 1 -t $(SEC) -D /mnt/temp
	sudo umount /dev/loop$(NUM)
	cat /proc/sloop/loop$(NUM)/wlimit_count
	sudo ./diskmig -x /dev/loop$(NUM) 1.map
	sudo ./diskmig -m 1.map
	sudo losetup -d /dev/loop$(NUM)
	sudo losetup /dev/loop$(NUM) 2.img
	sudo ./diskmig -r /dev/loop$(NUM) 1.map
	cat /proc/sloop/loop$(NUM)/rlimit_count
	sudo mount /dev/loop$(NUM) /mnt/temp
	sudo dbench 1 -t $(SEC) -D /mnt/temp
	sudo umount /dev/loop$(NUM)

show:
	@echo -n wlimit_count:\ 
	@cat /proc/sloop/loop$(NUM)/wlimit_count
	@echo -n rlimit_count:\ 
	@cat /proc/sloop/loop$(NUM)/rlimit_count
	@echo -n rblocked_count:\ 
	@cat /proc/sloop/loop$(NUM)/rblocked_count

showb:
	@echo rblocked:
	@cat /proc/sloop/loop$(NUM)/rblocked

kick:
	@echo kick rblocked:
	@cat /proc/sloop/loop$(NUM)/rblocked_kick

ddr:
	sudo dd if=/dev/loop$(NUM) of=/dev/null bs=4096 count=$(RC)

ddw:
	sudo dd if=/dev/zero of=/dev/loop$(NUM) bs=4096 count=$(WC)

l:
	sudo losetup /dev/loop$(NUM) 1.img

c:
	sudo ./diskmig -c /dev/loop$(NUM) 1.img

k:
	sudo losetup -d /dev/loop$(NUM)

m:
	sudo mount /dev/loop$(NUM) /mnt/temp

u:
	sudo umount /dev/loop$(NUM)

w:
	sudo ./diskmig -w /dev/loop$(NUM)

x:
	sudo ./diskmig -x /dev/loop$(NUM) 1.map

r:
	sudo ./diskmig -r /dev/loop$(NUM) 1.map

s:
	sudo ./diskmig -s /dev/loop$(NUM) 1.img

t:
	while cat /proc/sloop/loop$(NUM)/rblocked > /dev/null; \
	do \
		cat /proc/sloop/loop$(NUM)/rblocked | xargs -I num sudo ./diskmig -t /proc/sloop/loop$(NUM)/rblocked -b num 1.img > /dev/null; \
		echo -n . ; \
	done

t1:
	cat /proc/sloop/loop$(NUM)/rblocked | xargs -I num sudo ./diskmig -t /proc/sloop/loop$(NUM)/rblocked -b num 1.img

t2:
	cat /proc/sloop/loop$(NUM)/rlimit | xargs -I num sudo ./diskmig -t /proc/sloop/loop$(NUM)/rblocked -b num 1.img

rt1:
	cat /proc/sloop/loop$(NUM)/rblocked | sort -k1 -n -r | xargs -I num sudo ./diskmig -t /proc/sloop/loop$(NUM)/rblocked -b num 1.img

rt2:
	cat /proc/sloop/loop$(NUM)/rlimit | sort -k1 -n -r | xargs -I num sudo ./diskmig -t /proc/sloop/loop$(NUM)/rblocked -b num 1.img

ot1:
	cat /proc/sloop/loop$(NUM)/rblocked | xargs -I num sudo ./diskmig -t /dev/loop$(NUM) -b num 1.img

ot2:
	cat /proc/sloop/loop$(NUM)/rlimit | xargs -I num sudo ./diskmig -t /dev/loop$(NUM) -b num 1.img

ort1:
	cat /proc/sloop/loop$(NUM)/rblocked | sort -k1 -n -r | xargs -I num sudo ./diskmig -t /dev/loop$(NUM) -b num 1.img

ort2:
	cat /proc/sloop/loop$(NUM)/rlimit | sort -k1 -n -r | xargs -I num sudo ./diskmig -t /dev/loop$(NUM) -b num 1.img

d:
	dmesg | tail

test11: up 1.img l w ddw x r ddr

test12: up diskmig
	sudo rm 1.img 2.img || true
	sudo dd if=/dev/zero of=1.img bs=1M seek=40 count=1
	sudo dd if=/dev/zero of=2.img bs=1M seek=40 count=1
	sudo losetup /dev/loop$(NUM) 1.img
	sudo ./diskmig -w /dev/loop$(NUM)
	yes | sudo mkreiserfs /dev/loop0
	sudo ./diskmig -x /dev/loop$(NUM) 1.map
	sudo ./diskmig -m 1.map
	sudo losetup -d /dev/loop$(NUM)
	sudo losetup /dev/loop$(NUM) 2.img
	sudo ./diskmig -r /dev/loop$(NUM) 1.map
	sudo dd if=/dev/loop$(NUM) of=/dev/null
	od 1.img > 1.txt
	od 2.img > 2.txt
	diff 1.txt 2.txt
	rm 1.txt 2.txt
	sudo mount /dev/loop$(NUM) /mnt/temp
	sudo umount /dev/loop$(NUM)

test13: up diskmig
	sudo rm 1.img 2.img || true
	sudo dd if=/dev/zero of=1.img bs=1M seek=40 count=1
	sudo dd if=/dev/zero of=2.img bs=1M seek=40 count=1
	sudo losetup /dev/loop$(NUM) 1.img
	sudo ./diskmig -w /dev/loop$(NUM)
	yes | sudo mkreiserfs /dev/loop0
	sudo ./diskmig -x /dev/loop$(NUM) 1.map
	sudo ./diskmig -m 1.map
	sudo losetup -d /dev/loop$(NUM)
	sudo losetup /dev/loop$(NUM) 2.img
	sudo ./diskmig -r /dev/loop$(NUM) 1.map
	sudo mount /dev/loop$(NUM) /mnt/temp
	sudo umount /dev/loop$(NUM)

test14: up diskmig
	sudo rm 1.img || true
	sudo dd if=/dev/zero of=1.img bs=1M seek=40 count=1
	sudo losetup /dev/loop$(NUM) 1.img
	sudo ./diskmig -w /dev/loop$(NUM)
	yes | sudo mkreiserfs /dev/loop0
	sudo ./diskmig -c /dev/loop$(NUM) 1.img
	sudo mount /dev/loop$(NUM) /mnt/temp

db:
	sudo dbench 1 -t $(SEC) -D /mnt/temp

cc:
	sudo apt-get build-dep util-linux
	cd /mnt/temp; sudo apt-get source -b util-linux

test15: up diskmig
	sudo apt-get build-dep util-linux
	sudo rm 1.img 2.img || true
	sudo dd if=/dev/zero of=1.img bs=1M seek=200 count=1
	sudo dd if=/dev/zero of=2.img bs=1M seek=200 count=1
	sudo losetup /dev/loop$(NUM) 1.img
	sudo ./diskmig -w /dev/loop$(NUM)
	yes | sudo mkreiserfs /dev/loop0
	sudo mount /dev/loop$(NUM) /mnt/temp
	cd /mnt/temp; sudo apt-get source util-linux
	sudo ./diskmig -c /dev/loop$(NUM) 2.img
	cd /mnt/temp; sudo apt-get source -b util-linux
	sudo umount /dev/loop$(NUM)
	sudo losetup -d /dev/loop$(NUM)

test16: up diskmig 1.img
	sudo apt-get build-dep util-linux
	sudo rm 1.img 2.img || true
	sudo dd if=/dev/zero of=1.img bs=1M seek=200 count=1
	sudo dd if=/dev/zero of=2.img bs=1M seek=200 count=1
	sudo losetup /dev/loop$(NUM) 1.img
	sudo ./diskmig -w /dev/loop$(NUM)
	yes | sudo mkreiserfs /dev/loop0
	sudo mount /dev/loop$(NUM) /mnt/temp
	cd /mnt/temp; sudo apt-get source util-linux
	sudo umount /dev/loop$(NUM)
	sudo ./diskmig -x /dev/loop$(NUM) 1.map
	sudo ./diskmig -m 1.map
	sudo losetup -d /dev/loop$(NUM)
	sudo losetup /dev/loop$(NUM) 2.img
	sudo ./diskmig -r /dev/loop$(NUM) 1.map
	sudo mount /dev/loop$(NUM) /mnt/temp
	cd /mnt/temp; sudo apt-get source -b util-linux
	sudo umount /dev/loop$(NUM)
	sudo losetup -d /dev/loop$(NUM)

test: test16

endif

